package frontend.controller;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import backend.model.User;
import backend.service.ServiceException;
import backend.service.UserService;
import backend.service.UserServiceImpl;
import backend.util.ConnectionType;
import frontend.listeners.MainActionListener;
import frontend.listeners.MainMouseListener;
import frontend.listeners.MainTableListener;
import frontend.model.TableModel;
import frontend.util.InputValidator;
import frontend.view.MainFrame;

public class MainFrameController {

	private MainFrame frame;
	private UserService userService;
	private ConnectionType ct;
	private static MainFrameController instance = null;
	protected static boolean update;

	private MainFrameController() {
		ct = ConnectionType.HIBERNATE;
		this.frame = MainFrame.getInstance();
		this.userService = new UserServiceImpl(ct);
		TableModel.updateTable(frame, ct);
		frame.getCreate().addActionListener(new MainActionListener());
		frame.getUpdate().addActionListener(new MainActionListener());
		frame.getDelete().addActionListener(new MainActionListener());
		frame.getUpdatePopup().addActionListener(new MainActionListener());
		frame.getDeletePopup().addActionListener(new MainActionListener());
		frame.getTablePanel().getTable().getModel()
				.addTableModelListener(new MainTableListener());
		frame.getTablePanel().getTable()
				.addMouseListener(new MainMouseListener());
	}

	public static synchronized MainFrameController getInstance() {
		if (instance == null) {
			instance = new MainFrameController();
		}
		return instance;
	}

	public void create() {
		User user = new User();
		user.setUserId(0);
		new InsertFrameController(user, frame);
	}

	public void update() {
		JTable table = frame.getTablePanel().getTable();
		update = true;
		int rowId = table.getSelectedRow();
		if (rowId < 0 || rowId > table.getRowCount() - 1) {
			JOptionPane.showMessageDialog(frame, "Select an user to update");
			return;
		}
		Integer id = (Integer) table.getModel().getValueAt(rowId, 0);
		try {
			User user = userService.getUserById(id);
			new InsertFrameController(user, frame);
		} catch (ServiceException e) {
			JOptionPane.showMessageDialog(frame, e.getMessage());
		}

	}

	public void delete() {
		JTable table = frame.getTablePanel().getTable();
		int rowId = table.getSelectedRow();
		if (rowId < 0 || rowId > table.getRowCount() - 1) {
			JOptionPane.showMessageDialog(frame, "Select an user to delete");
			return;
		}
		Integer id = (Integer) table.getModel().getValueAt(rowId, 0);
		try {
			userService.deleteUser(id);
			TableModel.updateTable(frame, ct);
		} catch (ServiceException e) {
			JOptionPane.showMessageDialog(frame, e.getMessage());
		}

	}

	public void updateTabel() {
		JTable table = frame.getTablePanel().getTable();
		String message = InputValidator.validInput(
				(String) table.getValueAt(table.getSelectedRow(), 1),
				(String) table.getValueAt(table.getSelectedRow(), 2),
				(String) table.getValueAt(table.getSelectedRow(), 3),
				(String) table.getValueAt(table.getSelectedRow(), 4));
		if (message.equals("")) {
			String pass = userService.getUserPassword((Integer) table
					.getValueAt(table.getSelectedRow(), 0));
			User user = new User((String) table.getValueAt(
					table.getSelectedRow(), 1), (String) table.getValueAt(
					table.getSelectedRow(), 2), (String) table.getValueAt(
					table.getSelectedRow(), 3), (String) table.getValueAt(
					table.getSelectedRow(), 4), (String) table.getValueAt(
					table.getSelectedRow(), 5), (String) table.getValueAt(
					table.getSelectedRow(), 6), pass,
					(String) table.getValueAt(table.getSelectedRow(), 7));
			user.setUserId((Integer) table.getValueAt(table.getSelectedRow(), 0));
			try {
				userService.updateUser(user);
				TableModel.updateTable(frame, ct);
			} catch (ServiceException e) {
				JOptionPane.showMessageDialog(frame, e.getMessage());
			}

		} else {
			JOptionPane.showMessageDialog(frame, message);
			TableModel.updateTable(frame, ct);
			return;
		}
	}

	public void mouse(MouseEvent e) {
		Point p = e.getPoint();
		int rowNumber = frame.getTablePanel().getTable().rowAtPoint(p);
		ListSelectionModel model = frame.getTablePanel().getTable()
				.getSelectionModel();
		model.setSelectionInterval(rowNumber, rowNumber);
	}

}
